package cn.laixueit.controller;

import cn.laixueit.common.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/upload")
public class FileController {

    @PostMapping("/img")
    public @ResponseBody
    String img(MultipartFile file) {
        String oldName = file.getOriginalFilename();
        String uuid = Utils.uuid();
        String fileName = uuid + oldName;
        File newfile = new File("D:\\codeWork\\java209\\legoumall\\nginx-1.18.0\\image", fileName);
        try {
            file.transferTo(newfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:8081/" + fileName;
    }
}
